package com.wclass.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.wclass.entity.CouresTask;
import com.wclass.entity.CourseInfo;
import com.wclass.mapper.CouresTaskMapper;
import com.wclass.service.ICouresTaskService;
import com.wclass.service.ICourseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 课程作业 服务实现类
 * </p>
 *
 * @author 张浩高
 * @since 2018-12-20
 */
@Service
public class CouresTaskServiceImpl extends ServiceImpl<CouresTaskMapper, CouresTask> implements ICouresTaskService {

    @Autowired
    private ICourseInfoService iCourseInfoService;

    @Override
    public Map<String, Object> selectMapByCouresId(Long courseInfoId) {
        CourseInfo courseInfo = iCourseInfoService.selectById(courseInfoId);

        Map<String, Object> map = Maps.newHashMap();
        if (courseInfo != null) {
            EntityWrapper<CouresTask> ew = new EntityWrapper<>();
            ew.isNull("p_id").eq("course_info_id", courseInfoId);
            Integer count = baseMapper.selectCount(ew);
            map.put("couresId", courseInfoId);
            map.put("couresImg", courseInfo.getPictureAddress());
            map.put("couresName", courseInfo.getName());
            map.put("taskCount", count);
            map.put("finishCount", baseMapper.selectCount(new EntityWrapper<CouresTask>().isNotNull("p_id").eq("course_info_id", courseInfoId).eq("submit_state", 1)));
            map.put("unfinishCount", baseMapper.selectCount(new EntityWrapper<CouresTask>().isNotNull("p_id").eq("course_info_id", courseInfoId).eq("submit_state", 0)));
            map.put("correctCount", baseMapper.selectCount(new EntityWrapper<CouresTask>().isNotNull("p_id").eq("course_info_id", courseInfoId).eq("submit_state", 2)));
        } else {
            map.put("couresId", "--");
            map.put("couresName", "--");
            map.put("taskCount", "--");
            map.put("couresImg", "");
            map.put("finishCount", 0);
            map.put("unfinishCount", 0);
            map.put("correctCount", 0);

        }
        return map;
    }

    @Override
    public Integer getTaskCount(int state, Long userID, Long courseInfoId) {
        EntityWrapper<CouresTask> ew = new EntityWrapper<>();
        if (state == -1) {
            ew.isNotNull("info.p_id").eq("info.course_info_id", courseInfoId).eq("info.user_id", userID);
        } else {
            ew.isNotNull("info.p_id").eq("info.course_info_id", courseInfoId).eq("stuTask.submit_state", state).eq("info.user_id", userID);
        }

        return baseMapper.selectTaskCount(ew);
    }
}
